package com.epam.client.controller;

import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;

import com.epam.DAO.CarDAO;
import com.epam.DAO.RequestDAO;
import com.epam.DAO.Factories.DAOFactory;
import com.epam.entity.Car;
import com.epam.entity.Client;
import com.epam.entity.Request;
import com.epam.entity.Status;
import com.epam.utils.JsfUtil;

public class NewRequestBean {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ClientLoginBean.class);

	private DAOFactory daoFactory = DAOFactory
	.getDAOFactory(DAOFactory.CURRENT_IMPLEMENTATION);
	private RequestDAO requestDAO;
	private CarDAO carDAO;


	@SuppressWarnings("unused")
	private SelectItem[] cars;
	
	private Request request;
	private Car car;

	{
		daoFactory = DAOFactory
				.getDAOFactory(DAOFactory.CURRENT_IMPLEMENTATION);
		requestDAO = daoFactory.getRequestDAO();
		carDAO = daoFactory.getCarDAO();
		request = new Request();
	}
	
	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}
	
	public void setDAOFactory(DAOFactory daoFactory){
		this.daoFactory = daoFactory;
	}
	
	public void setCarDAO(CarDAO carDAO) {
		this.carDAO = carDAO;
	}
	
	public void setRequestDAO(RequestDAO requestDAO) {
		this.requestDAO = requestDAO;
	}
	
	public SelectItem[] getCars() {
		return JsfUtil.getSelectItems(carDAO.getAvaliableCars());
	}

	public String createRequest() {

		Client client = (Client) JsfUtil.getAttribute("client");

		car.setIsAvaliable(false);
		carDAO.update(car);

		request.setClient(client);
		request.setCar(car);
		request.setStatus(Status.NEW);
		requestDAO.create(request);

		JsfUtil.addMessage("new_request_form:beginDate", "Request successfully created");

		return "success";
	}

}
